# encoding: utf-8
# @File  : test09_ht_add_product.py
# @Author: wu shaofan
# @Date  :  2024/04/29
# @Desc :
import json
import random
import allure
from faker import Faker
import config
import conftest
from PO.page.page_ht_ProductList import PagehtProductList
from PO.page.page_ht_login import PageHtLogin
from Utils.get_driver import GetDriver
from Utils.record_log import Log


@allure.feature('5、后台商品管理')
class TestAddproduct:
    def setup_class(self):
        # 获取driver
        self.driver = GetDriver().get_driver(config.ht_login_url)
        # 异常截图
        conftest.driver = self.driver
        # 获取商品管理对象
        self.product_manage = PagehtProductList(self.driver)
        # 获取后台登录对象
        self.login = PageHtLogin(self.driver)
        # 创建一个Faker对象
        self.fake = Faker(locale='zh_CN')
        # 获取日志对象
        self.log = Log("添加商品用例").get_logger()

    def teardown_class(self):
        # TODO 关闭driver
        GetDriver().close_driver()
        Log("添加商品用例").write_empty_line()

    @allure.story('1、添加商品')
    def test_add_product(self):
        with allure.step('前置条件：登录测试账号'):
            self.login.page_ht_login()

        with allure.step('步骤一：点击商城tab菜单'):
            self.product_manage.page_ht_click_shop_tab()

        with allure.step('步骤二：进入iframe框架'):
            self.product_manage.page_ht_enter_iframe()

        with allure.step('步骤三：点击添加商品按钮'):
            self.product_manage.page_ht_click_add_product_btn()

        with allure.step('步骤四：输入商品名称'):
            goods_name = f"自动化商品_{self.fake.name()}"
            # 给config文件中add_product_name全局变量重新赋值
            config.add_product_name = goods_name
            self.product_manage.page_ht_input_product_name(goods_name)

        with allure.step('步骤五：滚动滑动条'):
            self.product_manage.base_move_scroll_bar(200)

        with allure.step('步骤六：选择一级商品分类'):
            category_level1 = self.product_manage.page_ht_select_product_category01()

        with allure.step('步骤七：选择二级商品分类'):
            # self.product_manage.base_time_sleep(0.1)
            category_level2 = self.product_manage.page_ht_select_product_category02()

        with allure.step('步骤八：选择三级商品分类'):
            # self.product_manage.base_time_sleep(0.1)
            category_level3 = self.product_manage.page_ht_select_product_category03()

        with allure.step('步骤九：输入商品简介'):
            goods_remark = self.fake.text()
            self.product_manage.page_ht_input_product_introduction(goods_remark)

        with allure.step('步骤十：输入商品SPU'):
            goods_spu = '12345{}'.format(random.randint(1, 99))
            self.product_manage.page_ht_input_product_spu(goods_spu)

        with allure.step('步骤十一：输入商品SKU'):
            goods_sku = '678910{}'.format(random.randint(1, 99))
            self.product_manage.page_ht_input_product_sku(goods_sku)

        with allure.step('步骤十二：选择商品品牌'):
            brand = self.product_manage.page_ht_select_product_brand()

        with allure.step('步骤十三：选择供应商'):
            supplier = self.product_manage.page_ht_select_product_supplier()
            self.product_manage.base_get_allure_image()

        with allure.step('步骤十四：输入商品价格'):
            goods_price = random.randint(50, 99)
            self.product_manage.page_ht_input_product_price(goods_price)

        with allure.step('步骤十五：输入商品市场价'):
            goods_market_price = random.randint(100, 999)
            self.product_manage.page_ht_input_product_market_price(goods_market_price)

        with allure.step('步骤十六：输入商品成本价'):
            goods_cost_price = random.randint(1, 50)
            self.product_manage.page_ht_input_product_cost_price(goods_cost_price)

        with allure.step('步骤十七：输入佣金'):
            goods_commission = random.randint(1, 50)
            self.product_manage.page_ht_input_product_commission(goods_commission)

        with allure.step('步骤十八：上传商品图片（二级iframe框架）'):
            filepath = config.BASE_DIR + r"/data/test04_head.jpg"
            # TODO 点击上传图片按钮
            self.product_manage.page_ht__upload_product_image_btn()
            # TODO 进入上传图片iframe框架（二级iframe框架）
            self.product_manage.page_ht_to_iframe5()
            # TODO 上传图片路径
            self.product_manage.page_ht_upload_product_image(filepath)
            # TODO 等待图片上传成功
            self.product_manage.page_ht_upload_success()
            # TODO 点击确认上传按钮
            self.product_manage.page_ht_click_upload_success_btn()
            # TODO 退出上传图片iframe框架
            self.driver.switch_to.default_content()
            # TODO 重新进入一级iframe框架
            self.product_manage.page_ht_enter_iframe()

        with allure.step('步骤十九：选择商品标签'):
            label = self.product_manage.page_ht_select_product_label()

        with allure.step('步骤二十：输入商品重量'):
            goods_weight = random.randint(1, 50)
            self.product_manage.page_ht_input_product_weight(goods_weight)

        with allure.step('步骤二十一：输入商品体积'):
            goods_volume = random.randint(1, 50)
            self.product_manage.page_ht_input_product_volume(goods_volume)

        with (allure.step('步骤二十二：选择运费模版')):
            tem = self.product_manage.page_ht_select_product_freight_template()

        with allure.step('步骤二十三：输入商品关键词'):
            goods_keywords = "自动化创建的商品"
            self.product_manage.page_ht_input_product_keywords(goods_keywords)

        with allure.step('步骤二十四：输入商品虚拟销售量'):
            goods_fictitious_sales = random.randint(1, 999)
            self.product_manage.page_ht_input_product_fictitious_sales(goods_fictitious_sales)

        with allure.step('步骤二十五：输入商品虚拟收藏量'):
            goods_fictitious_collects = random.randint(1, 999)
            self.product_manage.page_ht_input_product_fictitious_collect(goods_fictitious_collects)

        with allure.step('步骤二十六：输入商品描述'):
            goods_description = self.fake.text()
            self.product_manage.page_ht_enter_iframe3()
            # 进入iframe框架，添加描述
            self.product_manage.page_ht_input_product_description(goods_description)
            # 退出iframe框架
            self.driver.switch_to.default_content()
            # 重新进入一级iframe框架
            self.product_manage.page_ht_enter_iframe()

        with allure.step('步骤二十七：点击保存按钮'):
            self.product_manage.page_ht_click_save_btn()

        with allure.step('步骤二十八：获取toast信息'):
            toast_info = self.product_manage.page_ht_get_toast_info()
            self.product_manage.base_get_allure_image()

        with allure.step('步骤二十九：断言'):
            assert toast_info == "操作成功"

        data = {
            "添加商品数据": {
                "商品名称": goods_name,
                "商品分类": {
                    "一级分类": category_level1,
                    "二级分类": category_level2,
                    "三级分类": category_level3},
                "商品简介": goods_remark,
                "商品spu": goods_spu,
                "商品sku": goods_sku,
                "商品品牌": brand,
                "商品供应商": supplier,
                "本店售价": f"{goods_price}元",
                "市场价": f"{goods_market_price}元",
                "成本价": f"{goods_cost_price}元",
                "商品佣金": f"{goods_commission}元",
                "商品标签": label,
                "商品重量": f"{goods_weight}g",
                "商品体积": f"{goods_volume}立方米",
                "运费模版": tem,
                "商品关键词": goods_keywords,
                "商品虚拟销售量": f"{goods_fictitious_sales}件",
                "商品虚拟收藏量": f"{goods_fictitious_collects}件",
                "商品描述": goods_description,
                "toast信息": toast_info
            },
            "断言": {
                "预期结果": "操作成功",
                "实际结果": toast_info},
            "config文件中的全局变量": config.add_product_name
        }
        self.log.info(f"后台添加商品数据：{data}")
        data = json.dumps(data, ensure_ascii=False, indent=4)
        allure.attach(data, "后台添加商品数据", allure.attachment_type.JSON)
